﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.Model;
using System.Text.RegularExpressions;
using University.Workshop.DataAccess;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Interaction logic for UserBuyChipsCard.xaml
    /// </summary>
    public partial class UserBuyChipsCard : Window
    {
        UserChipsBuy userChipsBuy = new UserChipsBuy();
        User user =new User();
        public UserBuyChipsCard()//(User user)
        {
            InitializeComponent();
        }
        public UserBuyChipsCard(User userTwo)
        {
            InitializeComponent();
            user = userTwo;
        }
        private void buttonLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Code = textBoxCode.Text;
            string Name = textBoxUserName.Text;
            bool result = false;
            if (Name != "" && Code != "")
            {
                result = userChipsBuy.LogOnCreditCard(Code, Name);
                if (result)
                {
                    radioButtonConsultAmount.Visibility = Visibility.Visible;
                    radioButtonBuyChips.Visibility = Visibility.Visible;
                    radioButtonSellChips.Visibility = Visibility.Visible;
                    buttonConsult.IsEnabled = true;
                    buttonConsult.Visibility = Visibility.Visible;
                    textBoxAmount.Visibility = Visibility.Visible;
                    label1.Visibility = Visibility.Visible;
                    label2.Content = "Welcome";
                    label2.Visibility = Visibility.Visible;

                    textBoxUserName.Visibility = Visibility.Hidden;
                    textBoxCode.Visibility = Visibility.Hidden;
                    buttonLogIn.IsEnabled = false;
                    buttonLogIn.Visibility = Visibility.Hidden;
                    labelCode.Visibility = Visibility.Hidden;
                    labelUserName.Visibility = Visibility.Hidden;
                }
                else
                {
                    labelErrorLogIn.Content = "User Does not Found";
                    textBoxUserName.Text = " ";
                    textBoxCode.Text = " ";
                }
            }
            else
            {
                labelErrorLogIn.Content = "Can´t contein spaces or can't be empty";
            }
        }

        private void buttonConsult_Click(object sender, RoutedEventArgs e)
        {
            string Code = textBoxCode.Text;
            string Name = textBoxUserName.Text;
            string Amount = textBoxAmount.Text;
            string result = "";
            if (radioButtonConsultAmount.IsChecked == true)
            {
                result = userChipsBuy.ConsultAmount(Code, Name);
                if (result == "Doesn'tFound")
                    labelResultConsult.Content = result;
                else
                    labelResultConsult.Content = "Amount " + result;
            }
            else
            {
                if (radioButtonBuyChips.IsChecked == true && textBoxAmount.Text != "" && IsNumber(Amount))
                {
                    result = userChipsBuy.ChangeAmount(Code, Amount, "res", Name);
                    if (result == "false")
                    {
                        labelResultConsult.Content = "Buy Chips " + "( " + result + " )";
                        label3.Content = "Insufficient money";
                    }
                    else
                    {
                        labelResultConsult.Content = "Buy Chips " + "( " + result + " )";
                        label3.Content = "Money transferred";
                        user.Chips = user.Chips - Convert.ToInt64(Amount);  //modificarrrrrrrrrrrrrrrrrrrrrrr
                    }
                }
                else
                {
                    if (radioButtonSellChips.IsChecked == true && textBoxAmount.Text != "" && IsNumber(Amount) && !textBoxAmount.Text.Contains(" "))
                    {
                        if(user.Chips > Convert.ToInt64(Amount))
                        {
                            result = userChipsBuy.ChangeAmount(Code, Amount, "sum", Name);
                            if (result == "false")
                            {
                                labelResultConsult.Content = "Sell Chips " + "( " + result + " )";
                                label3.Content = "Insufficient chips";
                            }
                            else
                            {
                                labelResultConsult.Content = "Sell Chips " + "( " + result + " )";
                                label3.Content = "Chips transferred";
                                user.Chips = user.Chips - Convert.ToInt64(Amount);
                            }
                        }
                        else
                        {
                            labelResultConsult.Content = "Sell Chips " + "( " + result + " )";
                            label3.Content = "Insufficient chips";
                        }
                    }
                    else
                    {
                        labelResultConsult.Content = "Can´t contein spaces or can't be empty";
                        label3.Content = "";
                        
                    }
                }
                
            }
        }

        private void textBoxUserName_TextChanged(object sender, TextChangedEventArgs e)
        {
            labelErrorLogIn.Content="";
            if (textBoxUserName.Text.Contains(" "))
            {
                labelErrorLogIn.Content = "Can´t contein spaces or can't be empty";
            }
            else
            {
                if ((textBoxUserName.Text).Length <= 4 || (textBoxUserName.Text).Length > 30)
                {
                    buttonLogIn.IsEnabled = false;
                    labelErrorLogIn.Content = "The User Name is too short or long";
                }
                else
                {
                    if (textBoxCode.Text != "")
                        buttonLogIn.IsEnabled = true;
                }
            }
        }

        private void textBoxCode_TextChanged(object sender, TextChangedEventArgs e)
        {
            labelErrorLogIn.Content = "";
            if (textBoxCode.Text.Contains(" "))
            {
                labelErrorLogIn.Content = "Can´t contein spaces or can't be empty";
            }
            else
            {
                if ((textBoxCode.Text).Length > 5)
                {
                    buttonLogIn.IsEnabled = false;
                    labelErrorLogIn.Content = "The Code is too long";
                }
                else
                {
                    if (textBoxUserName.Text != "")
                        buttonLogIn.IsEnabled = true;
                }
            }
        }
       
        public bool IsNumber(string inputvalue)
        {
            Regex isnumber = new Regex("[^0-9]");
            return !isnumber.IsMatch(inputvalue);
        }
    }
}
